﻿using System;
using System.Net;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using Codeblack.TfsScrumboard.Interfaces;
using Codeblack.TfsScrumboard.ViewModels;
using Microsoft.Practices.Unity;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WebAccess.Common;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace Codeblack.TfsScrumboard.Services
{
    /// <summary>
    /// Webservice for retrieving scrumboard data from script.
    /// </summary>
    [WebService(Namespace = "http://codeblack.nl/tfsscrumboard/ScrumboardWebService/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class ScrumboardWebService : WebService
    {
        #region Constructor

        public ScrumboardWebService()
        {
            Codeblack.TfsScrumboard.Runtime.Container.Instance.BuildUp(this);
        }

        #endregion

        #region Dependencies

        [Dependency]
        public IScrumboardService ScrumboardService { get; set; }

        #endregion

        #region Webmethods

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public PlanningViewModel GetPlanning(string backlogName)
        {
            Project project = GetCurrentProject(this.Context.Request);
            if (project != null)
            {
                return this.ScrumboardService.GetPlanning(project, backlogName);
            }
            else
            {
                return new PlanningViewModel()
                {
                    BacklogName = backlogName,
                    QueryResults = new QueryResultsViewModel()
                    {
                        TotalWorkItems = 0,
                        TotalStoryPoints = 0.0
                    },
                    WorkItems = new WorkItemViewModel[0]
                };
            }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public PlanningViewModel UpdatePriority(string backlogName, int id, int? previousId, int? nextId)
        {
            Project project = GetCurrentProject(this.Context.Request);
            if (project != null)
            {
                return this.ScrumboardService.UpdatePriority(project, backlogName, id, previousId, nextId);
            }

            return null;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public PlanningViewModel UpdatePlanning(string backlogName,int id, string updateBacklogName)
        {
            Project project = GetCurrentProject(this.Context.Request);
            if (project != null)
            {
                return this.ScrumboardService.UpdatePlanning(project, backlogName, id, updateBacklogName);
            }

            return null;
        }

        #endregion

        #region Private methods

        private static Project GetCurrentProject(HttpRequest request)
        {
            Uri serverUrl = TSWAConfiguration.Current.DefaultTfsLocator.ServerUrl;
            TfsLocator projectLocation = TfsLocator.CreateFromPairs(request.Params);

            TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(serverUrl);
            WorkItemStore workitemStore = teamProjectCollection.GetService<WorkItemStore>();

            Project currentProject = null;
            foreach (Project project in workitemStore.Projects)
            {
                Guid projectGuid = TfsLocator.CreateProjectGuidFrom(project.Uri);
                if (projectGuid == projectLocation.ProjectGuid)
                {
                    currentProject = project;
                    break;
                }
            }

            return currentProject;
        }

        #endregion
    }
}
